#!/usr/bin/env python
#NMmisc

import sys
import os
#import shutil
#import StringIO
import string 
#import re
import difflib
#import csv
 
#try:
#   #import cPickle as pickle
#except:
import pickle
#sys.setdefaultencoding('utf-8')
 

def ut_iif(testexpr, trueval, falseval):
    return trueval if testexpr else falseval
    #osbolete return (uTrue, uFalse )[ not bCondition ]
def ut_setType(val, tp):
    #tp = example of type 
    tp=str(type(tp))
    tp=tp.split(' ')[1].split('>')[0]
    tp=tp[1:-1] + '(val)'
    return eval(tp)

def ut_lstbreakdown(ls, rt=[]):
    if isinstance(ls,(list,tuple)):
        for it in ls:ut_lstbreakdown(it,rt)
    else:
        rt.append(ls) 
    return rt   
def ut_lstToTable(L,delim1='\t',delim2='\n'):           # Lists of list as tab delimited
    rt=''
    for item  in L:
        rt=rt+ delim1.join(map(str, item)) + delim2 
    return rt
def ut_lstfreq(lst):
    return [(a, lst.count(a)) for a in lst]
def ut_lstcomp(lst):
    return [[x,y] for x in lst for y in lst if x!=y]

def ut_closematchesLst(l,numbOfitems=3,sensitivity=0.6):
    rt=[]
    for item in l:
        mtc=difflib.get_close_matches(item, l,numbOfitems,sensitivity)
        rt.append(mtc) 
    for item in rt:
        cnt = 0
        for freq in rt:
            for subitem in freq:
                if item[0]==subitem:cnt +=1
        item.insert(0, cnt )          
    return rt
def ut_closeMatches1L(lst,PosibleValslst,numbOfitems=1,sensitivity=0.6):
    rt=[]
    for item in lst:
        mtc=difflib.get_close_matches(item,PosibleValslst,numbOfitems,sensitivity)
        rt.extend(mtc)
    return rt   
def ut_closeMatches(lst,PosibleValslst,numbOfitems=1,sensitivity=0.6):
    rt=[]
    for item in lst:
        mtc=difflib.get_close_matches(item,PosibleValslst,numbOfitems,sensitivity) 
        rt.append(mtc) 
    for item in rt:
        cnt = -1
        for freq in rt:
            for subitem in freq:
                if item[0]==subitem:cnt +=1
        item.insert(0, cnt )          
    return rt
def ut_delay(X):
    for dummy in range(X):
        pass    
#####   vvvv  file ops vvvvvvv
def fl_readstr(fpath):
    fin=open(fpath)
    str=fin.read()
    fin.close()
    return str
def fl_renameext(directory,newext='.bkp'):
    for path in os.listdir(directory):
        os.rename(directory +  path , directory  + path + newext)
def fl_GetUniqueLines(flpath1 , flpath2, startFromLine=0): 
    fin1=open( flpath1, 'r')
    fin2=open( flpath2, 'r')
    set1=set(fin1.readlines()[startFromLine:]) 
    set2=set(fin2.readlines()[startFromLine:]) 
    fin1.close()
    fin2.close()
    return [set1 -set2, set2-set1]
def fl_write(path, contents):
    fl=open(path, 'w')
    fl.write(contents)
    fl.close 

def fl_split(path, maxlines=100000):
    fin=open( path, 'r')
    fout=None
    linenum = -1
    filenum=0
    of=path.split('.')
    if len(of) > 1 :
        ext='.'+ of[1]
    else:
        ext=''
    of=of[0]
    for line in fin:
        linenum += 1 
        if (linenum % maxlines== 0):
            if filenum !=0:
                fout.close()
            filenum += 1
            outpath=of + '_' + str(filenum) +  ext 
            print 'writing to:'+ outpath
            fout=open(outpath, 'w')
        fout.write(line)
    if (linenum % maxlines != 0):
        fout.close()
    fin.close()
    print 'total lines:' + str (linenum + 1 )
    return True
def fl_skiplines(path, linestart,lineend):
    fin=open( path, 'r')
    linenum = 0
    of=path.split('.')
    if len(of) > 1 :
        ext='.'+ of[1]
    else:
        ext=''
    of=of[0]
    outpath=of + '_Skiped'+str(linestart)+ 'to' +str(lineend)  +  ext 
    print 'writing to:'+ outpath
    fout=open(outpath, 'w')
    
    for line in fin:
        linenum += 1 
        if (linenum % 100000 == 0):
            print 'writing line:'+ str(linenum)
        if (linenum < linestart) | (linenum > lineend):
            fout.write(line)
    fout.close()
    fin.close()
    print 'total lines:' + str (linenum   )
    return True
#####   vvvv  str  ops vvvvvvv   
def str_between(str, startmarker, endmarker):
    l=str.split(startmarker)
    if len(l) >1:
        l=l[1].split(endmarker)
        if len(l)>1:
            return l[0]
    return ''
def str_abrev(str,maxchrs):
    r=str.split()
    rt=''
    ln=len(r)
    for s in r:
        v=(maxchrs-len(rt))/ln
        rt += s[0:v].title()
        ln += -1
    return rt[0:maxchrs]
def str_ascii_lettersonly(str,replacechr=''):
    rt=''
    for l in str: 
        if (string.ascii_letters.find(l) < 0):
            rt += replacechr
        else:
            rt +=l
    return rt   
def str_numericonly(str,replacechr=''):
    (rt, digits) =('',string.digits + '+-.,') 
    for l in str: 
        if (digits.find(l) < 0):
            rt += replacechr
        else:
            rt +=l
    return rt   
def str_clear_ntr(str):
    return str.replace('\n','').replace('\t','').replace('\r','')
#####   vvvv  misc vvvvvvv      
def bit_contains (numb,nums): 
    """bit_contains(3,1) = True"""
    return (numb & nums == nums)     
def pcl_tofile(obj,pathname):
    rt=True
    fl = open(pathname, 'wb')
    try:   pickle.dump(obj, fl,-1)
    except:rt=sys.exc_info() 
    fl.close()
    return rt
def pcl_fromfile(pathname): 
    try:
        fl = open(pathname, 'rb')
        rt = pickle.load(fl)
    except:
        rt= sys.exc_info() 
    finally:
        fl.close()
    return rt

######### #####################################################    
